package com.lanyun.auth.controller.admin.authUserRole;


import com.google.common.base.Preconditions;
import com.lanyun.auth.controller.admin.authUserRole.vo.AuthUserRoleRequestVO;
import com.lanyun.auth.domain.authUserRole.AuthUserRoleDomainService;
import com.lanyun.web.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/12
 */
@RestController
@RequestMapping("/api-admin/userRole")
@Slf4j
public class AuthUserRoleController {

    @Resource
    private AuthUserRoleDomainService authUserRoleDomainService;


    /**
     * 关联用户角色
     *
     * @param authUserRoleRequestVO
     * @return
     */
    @PostMapping("/add")
    public Result addUserRole(@RequestBody AuthUserRoleRequestVO authUserRoleRequestVO) {

        try {

            Preconditions.checkNotNull(ObjectUtils.isEmpty(authUserRoleRequestVO), "参数不能为空");
            Preconditions.checkNotNull(CollectionUtils.isEmpty(authUserRoleRequestVO.getRoleIds()), "角色主键不能为空");
            Preconditions.checkNotNull(authUserRoleRequestVO.getUserId(), "用户主键不能为空");

            Boolean result = authUserRoleDomainService.addUserRole(authUserRoleRequestVO);

            if (result) return Result.success(result);

            return Result.fail(result);

        } catch (IllegalArgumentException iae) {
            log.error("UserRoleController.addUserRole.error:", iae.getMessage(), iae);
            return Result.fail(iae.getMessage());
        }

    }

}
